package we.caro;

import java.util.List;

import we.core.bean.CDataContainer;
import we.core.bean.CUser;

public class CaroUtility {
    protected CaroUtility() {
    }
    private static CaroUtility _instance = new CaroUtility();

    public static CaroUtility instance() {
        return _instance;
    }

    public CUser findCompetitor(CUser u) {
        CUser result = null;
        int exp = Integer.parseInt(u.properties.get("exp"));
        int betLv = Integer.parseInt(u.properties.get("betLv"));
        List<CUser> userList = CDataContainer.instance().getUsers();
        int minAbs = Integer.MAX_VALUE;
        for (CUser user : userList) {
            if (!user.username.equals(u.username)) {
                // á Á
                String active = user.properties.get("active");
                if (active == null)
                    active = "false";
                if (active.equals(String.valueOf(true))) {
                    // doi thu cung dang tim kiem nguoi choi
                    int betLvNew =
                        Integer.parseInt(user.properties.get("betLv"));
                    if (betLv == betLvNew) {
                        int expNew =
                            Integer.parseInt(user.properties.get("exp"));
                        int abs = Math.abs(expNew - exp);
                        if (minAbs > abs) {
                            minAbs = abs;
                            result = user;
                        }
                    }
                }
            }
        }
        return result;
    }
}
